<?php

declare (strict_types=1);
namespace App\Model;

use Lete\Base\Abstraction\Model;
use Lete\Base\Traits\Model\Tree;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property int $id
 * @property int $parent_id
 * @property string $name
 * @property string $route
 * @property int $order
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property \Carbon\Carbon $deleted_at
 * @property string $include_sites
 * @property string $exclude_sites
 */
class AdminMenu extends Model
{
    use Tree;
    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'admin_menus';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'parent_id' => 'integer',
        'order' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
        'route' => 'array',
    ];
    /**
     * @var array
     */
    protected $guarded = ['id', 'created_at', 'updated_at', 'deleted_at'];

    /**
     * @param $value
     */
    public function setIncludeSitesAttribute($value)
    {
        $this->attributes['include_sites'] = is_array($value) ? json_encode($value) : $value;
    }

    /**
     * @param $value
     */
    public function setExcludeSitesAttribute($value)
    {
        $this->attributes['exclude_sites'] = is_array($value) ? json_encode($value) : $value;
    }

    /**
     * @param $value
     * @return string
     */
    public function getIncludeSitesAttribute($value)
    {
        $array = is_array($value) ? $value : ($value ? json_decode($value, true) : []);
        return implode(',', $array);
    }

    /**
     * @param $value
     * @return string
     */
    public function getExcludeSitesAttribute($value)
    {
        $array = is_array($value) ? $value : ($value ? json_decode($value, true) : []);
        return implode(',', $array);
    }
}
